package com.wkhyapp.lm.adapter;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.wkhyapp.lm.R;
import com.wkhyapp.lm.http.vo.Goods;
import com.wkhyapp.lm.utils.ImageLoadUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fang on 2021/10/24.
 */

public class GrabAlbumAdapter  extends BaseQuickAdapter<Goods, BaseViewHolder> {

    // 数据集
    private List<Goods> list = new ArrayList<Goods>();
    // 上下文
    // 存储勾选框状态的map集合
    private Map<Integer, Boolean> isCheck = new HashMap<Integer, Boolean>();


    public GrabAlbumAdapter(int layoutResId, List<Goods> data) {
        super(layoutResId, data);
        // 默觉得不选中
        initCheck(false);
    }

    @Override
    protected void convert(final BaseViewHolder helper, final Goods item) {
        ImageView imageView = helper.getView(R.id.g_iv);
        ImageLoadUtil.setRoundImage_Normal(mContext,item.getImg(),6,imageView);
        TextView title = helper.getView(R.id.title_tv);
        title.setText(Html.fromHtml(item.getDesc()));
      // helper.setText(R.id.title_tv,item.getDesc());
        CheckBox share_cb = helper.getView(R.id.share_cb);
        share_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isCheck.put(helper.getPosition(), isChecked);
            }
        });
        // 设置状态
        if (isCheck.get(helper.getPosition()) == null) {
            isCheck.put(helper.getPosition(), false);
        }
        share_cb.setChecked(isCheck.get(helper.getPosition()));
        if(item.isExist()){
            helper.getView(R.id.ex_tv).setVisibility(View.VISIBLE);
            share_cb.setEnabled(false);
        }else{
            helper.getView(R.id.ex_tv).setVisibility(View.GONE);
            share_cb.setEnabled(true);
        }
    }

    // 初始化map集合
    public void initCheck(boolean flag) {
        // map集合的数量和list的数量是一致的
        for (int i = 0; i < list.size(); i++) {
            // 设置默认的显示
             if(!list.get(i).isExist()){
                 isCheck.put(i, flag);
             }

        }
    }
    // 设置数据
    public void setData(List<Goods> data) {
        this.list = data;
    }

    // 全选button获取状态
    public Map<Integer, Boolean> getMap() {
        // 返回状态
        return isCheck;
    }

}
